package de.QuizCommunity.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import de.QuizCommunity.domain.Antwort;
import de.QuizCommunity.domain.Frage;

public class Frage_Put {
	public static void postFrage(Frage frage) {
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://www.iwi.hs-karlsruhe.de/ebatc/QuizCommunity/rest/webservice/frage");

		try {
			// Add your data

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			
			nameValuePairs.add(new BasicNameValuePair("benutzer", ""
					+ frage.getBenutzerId()));
			nameValuePairs.add(new BasicNameValuePair("fragestellung", frage
					.getFragestellung()));
			nameValuePairs.add(new BasicNameValuePair("gesperrt", ""
					+ frage.getGesperrt()));
			nameValuePairs.add(new BasicNameValuePair("version", ""
					+ frage.getVersion()));
			nameValuePairs.add(new BasicNameValuePair("einstellungs_Datum", ""
					+ frage.getEinstellungsdatum()));
			nameValuePairs.add(new BasicNameValuePair("stadt", ""
					+ frage.getStadt()));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
			httppost.setEntity(entity);
			httppost.setHeader("Content-type",
					"application/x-www-form-urlencoded; charset=UTF8");

			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);
			for(int i=0;i<=2;i++)
			 {
				 Antwort_Put.postAntwort(frage.getAntwortList().get(i));
			 }

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
	}
}
